﻿#include "IEventBridge.h"
#include "MyPublicMacro.h"

#include "GnsClientFrame.h"

IEventBridge*  IEventBridge::_pEvent = nullptr;

IEventBridge::IEventBridge()
{
}

IEventBridge::~IEventBridge()
{
}

IEventBridge* IEventBridge::getInstance()
{
	if (_pEvent == nullptr)
	{
		_pEvent = IEventBridge::create();
		assert(_pEvent);
	}

	return _pEvent;
}

IEventBridge* IEventBridge::create()
{
	IEventBridge *_pEvent = new(std::nothrow) IEventBridge();
	if (_pEvent && _pEvent->init())
	{
		return _pEvent;
	}
	else
	{
		delete _pEvent;
		_pEvent = nullptr;
		return nullptr;
	}
}

void IEventBridge::destoryInstance()
{
	if (_pEvent != nullptr)
	{
		delete _pEvent;
		_pEvent = nullptr;
	}
}

bool IEventBridge::init()
{
	return true;
}

// 开始启动事件桥梁
void IEventBridge::start()
{
	bRuning = true;
}

// 停止事件桥梁
void IEventBridge::stop()
{
	bRuning = false;
}

// 加入事件消息
bool IEventBridge::pushBack(const EVENT_INFO& eventInfo)
{
	std::lock_guard<std::recursive_mutex> guard(_mutexEventMsg);	// 加锁

	_dequeEventMsg.push_back(eventInfo);

	return true;
}

// 提取事件消息
bool IEventBridge::popFront(EVENT_INFO& eventInfo)
{
	if (!bRuning)
		return false;

	std::lock_guard<std::recursive_mutex> guard(_mutexEventMsg);	// 加锁

	if (bRuning && _dequeEventMsg.size() > 0)
	{
		eventInfo = _dequeEventMsg.front();
		_dequeEventMsg.pop_front();
		return true;
	}

	return false;
}